package com.google.code.drift.activity;

import java.util.ArrayList;
import java.util.Collection;

import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.code.drift.R;
import com.google.code.drift.adapter.NearbyPersonListAdapter;
import com.google.code.drift.api.Refreshable;
import com.google.code.drift.api.UserInfo;

public class ListTabActivity extends AbstractActivity implements Refreshable {

	ListView nearbyPersonList;
	TextView noPersonAlertText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_tab_layout);

		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		setupView();
	}

	@Override
	public void refresh() {
		setupView();
	}
	
	private void initView() {
		nearbyPersonList = (ListView) findViewById(R.id.person_list);
		noPersonAlertText = (TextView) findViewById(R.id.no_person_alert);
	}

	private void setupView() {
		Collection<UserInfo> userList = new ArrayList<UserInfo>(app.getMyInfo()
				.getNearbyPersonListFromHashMap());
		if (userList.size() == 0) {
			setAlertText();
		} else {
			setPersonList(userList);
		}
		userList = null;
	}

	private void setAlertText() {
		noPersonAlertText.setVisibility(View.VISIBLE);
		nearbyPersonList.setVisibility(View.GONE);
	}

	private void setPersonList(Collection<UserInfo> userList) {
		noPersonAlertText.setVisibility(View.GONE);
		nearbyPersonList.setVisibility(View.VISIBLE);
		BaseAdapter nearbyAdapter = new NearbyPersonListAdapter(this,
				(ArrayList<UserInfo>) userList);
		nearbyPersonList.setAdapter(nearbyAdapter);
	}

}
